unit BookSearch;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Db, Grids, DBGrids, IBQuery, DBCtrls, DBCGrids, ExtCtrls, Menus,
  ComCtrls;

type
  TfmBookSearch = class(TForm)
    btnSearch: TButton;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    TabSheet3: TTabSheet;
    Memo1: TMemo;
    Label6: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    AWTOR: TEdit;
    NAZW: TEdit;
    GOD: TEdit;
    SERIYA: TEdit;
    Label1: TLabel;
    INW_N: TEdit;
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    procedure FormCreate(Sender: TObject);
    procedure btnSearchClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure DBGrid1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure DBGrid1DblClick(Sender: TObject);
    procedure AWTORKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
    FWasShown: boolean;
    FQuery: TIBQuery;
    procedure LocateRec;
    procedure SetPageFocus(CtrlName: string);
  public
    { Public declarations }
  end;

var
  fmBookSearch: TfmBookSearch;

implementation

uses MainDataModule, HyperStr;
{$R *.DFM}

procedure TfmBookSearch.FormCreate(Sender: TObject);
begin
  FQuery := dmMain.qryBookSearch;
end;

procedure TfmBookSearch.FormShow(Sender: TObject);
var s: string;
begin
  //Width := Screen.Width;
  s := Ini.ReadString(Name, 'ActiveControl', '');
  if not FWasShown then
    SetPageFocus(s);
  FWasShown := True;
end;

procedure TfmBookSearch.btnSearchClick(Sender: TObject);
var
  i: integer;
  cn, s, s1, s2, v, v1: string;
  //Child: TfmResult;
  Ctrl: TControl;
begin
  with FQuery do begin
    Close;
    SQL.Clear;
    SQL.Add('SELECT * FROM SPISANIE');
    for i := 1 to PageControl1.ActivePage.ControlCount - 1 do begin
      Ctrl := PageControl1.ActivePage.Controls[i];
      v := TEdit(Ctrl).Text;
      v1 := AnsiUpperCase(Copy(v, 1, 1)) + Copy(v, 2, Length(v) - 1);
      if (Ctrl.Tag = 2) and (v <> '') then begin
        if SQL.Count = 1 then s := 'WHERE ' else s := 'AND ';
        cn := Ctrl.Name;
        s1 := cn + ' ' + Ctrl.Hint;
        if (cn = 'NAZW') then
          s2 := '(' + s1 + ' ''' + v + ''' OR ' + s1 + ' ''' + v1 + ''')'
        else if (cn = 'INW_N') then
          s2 :=  s1 + ' ' + v
        else
          s2 :=  s1 + ' ''' + v + '''';
        SQL.Add(s + s2);
      end;
    end;
    Memo1.Lines := SQL;
    //exit;
    Open;
    //Last;
    //if not BOF then
    //  ScrollBar1.Max := RecordCount
    //else
    //  ScrollBar1.Max := 1;
    //First;
    //ScrollBar1.Position := 1;
  end;
  //DisplayData;
  Ini.WriteString(Name, 'ActiveControl', ActiveControl.Name);
  DBGrid1.SetFocus;
end;


procedure TfmBookSearch.DBGrid1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = 13 then LocateRec;
end;

procedure TfmBookSearch.DBGrid1DblClick(Sender: TObject);
begin
  LocateRec;
end;

procedure TfmBookSearch.LocateRec;
begin
  dmMain.qrySpisanie.Locate('INW_N', DBGrid1.DataSource.DataSet['INW_N'], []);
  Hide;
end;

procedure TfmBookSearch.SetPageFocus(CtrlName: string);
var
  ts: TTabSheet;
  comp: TComponent;
  Ctrl: TWinControl;
  h: HWnd;
begin
  comp := FindComponent(CtrlName);
  if comp <> nil then begin
    h := TWinControl(comp).Handle;
    Ctrl := FindControl(h);
    if (Ctrl.Parent is TTabSheet) then begin
      ts := Ctrl.Parent as TTabSheet;
      if ts.PageControl.ActivePage <> ts then
        ts.PageControl.ActivePage := ts;
    end;
    Ctrl.SetFocus;
  end;
end;

procedure TfmBookSearch.AWTORKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = 13 then
    btnSearch.Click;
end;

end.
